package com.woniu.user.controller;


import com.alipay.api.domain.LoginForm;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu.user.commons.Result;
import com.woniu.user.controller.form.CheckTimeForm;
import com.woniu.user.model.Book;
import com.woniu.user.model.Coach;
import com.woniu.user.service.CoachService;
import com.woniu.user.service.dto.CoachInfoDto;
import com.woniu.user.util.JwtUtil;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;

/**
 * <p>
 * 教练特有信息表 前端控制器
 * </p>
 *
 * @author Silas
 * @since 2023-02-15
 */
@RestController
@RequestMapping("/coach")
@Slf4j
public class CoachController {

    @Resource
    private CoachService coachService;

    @GetMapping("/my")
    public Result myInfo(Integer accountId) {
        Coach coach = coachService.getOne(new QueryWrapper<Coach>().eq("account_id", accountId));
        return Result.success(200, "查询成功", coach);
    }

    // 更改教练流派
    @PostMapping("/setStyle")
    public Result setStyle(@RequestBody String style, HttpServletRequest req) {
        Integer userId = JwtUtil.getUserIdFromReq(req);// 获取用户账号
        coachService.setStyle(userId, style);
        return Result.success(200, "设置成功", null);
    }

    @GetMapping("/query")
    public Result query(String condition, Integer current, Integer size) {
        log.info("条件查询{}", condition);
        if ("".equals(condition) || condition == null) {
            Page<Coach> pageInfo = new Page<>(current == null ? 1 : current, size == null ? 4 : size);
            IPage<CoachInfoDto> allCoach = coachService.findAll(pageInfo);
            return Result.success(200, "查询成功", allCoach);
        } else {
            Page<CoachInfoDto> query = coachService.query(condition, current, size);
            return Result.success(200, "查询成功", query);
        }

    }

    // 查询所有教练
    @GetMapping("/findAll")
    public Result findAll(Integer page, Integer size) {
        log.info("查询所有教练信息{} {}", page, size);
        Page<Coach> pageInfo = new Page<>(page, size);
        IPage<CoachInfoDto> allCoach = coachService.findAll(pageInfo);
        return Result.success(200, "查询成功", allCoach);
    }

    // 用户预约（包括时间检查）
    @SneakyThrows
    @GetMapping("checkTime")
    public Result checkTime(CheckTimeForm form, HttpServletRequest req) {
        log.info("预约时间{}", form);
        Integer userId = JwtUtil.getUserIdFromReq(req);
        coachService.checkTime(form, userId);
        return Result.success(200, "预约成功", null);
    }

    //通过coach_id找到教练
    @GetMapping("/findCoach")
    public Result findCoach(Integer coachId) {
        log.info("查询id为{}的教练", coachId);
        Coach coach = coachService.getById(coachId);
        return Result.success(200, "查询成功", coach);
    }

    @GetMapping("/myCoach")
    public Result myCoach(Integer venueId) {
        List<Coach> coaches = coachService.myCoach(venueId);
        return Result.success(200, "查询成功", coaches);
    }
}

